import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

import { TreeNode } from 'primeng/primeng';
import { Subject } from 'rxjs';
import { Observable } from 'rxjs';
import { map } from "rxjs/operators";
@Injectable()
export class RoleService {

    /**用于在路由的环境下与父组件通讯 */
    private routeMsgPushSource = new Subject<any>();
    routeMsgPush$ = this.routeMsgPushSource.asObservable();

    constructor(private http: HttpClient) { }

    /** 获取组织机构岗位树 */
    getUnitRoleTree(unitId: number, isRoot: Boolean) {
        return this.http.get('/system/api/roleinfo/listtree?unitId=' + unitId + "&isRoot=" + isRoot).pipe(
            map((res:Response) => res['data'])
        )
    }

    /** 根据名称查询机构或岗位 */
    searchUnitRoleByName(name: string) {
        return this.http.get('/system/api/roleinfo/search?name=' + name).pipe(
            map((res:Response) => res['data'])
        )
    }
    /** 根据机构查询岗位信息 */
    getRolesByUnitId(id: number) {
        return this.http.get('/system/api/roleinfo/findrolesbyunitid?id=' + id).pipe(
            map((res:Response) => res['data'])
        )
    }
    /**根据主键查询岗位信息 */
    loadRoleById(role: any) {
        let url = (role.roleId ? '/system/api/roleinfo/load?id=' + role.roleId : '/system/api/roleinfo/showadd?unitId=' + role.unitId);
        return this.http.get(url).pipe(
            map((res:Response) => res['data'])
        )
    }
    /** 保存或新增岗位信息 */
    saveUpdateRole(roleInfo: any) {
        return this.http.post('/system/api/roleinfo/saveupdate', roleInfo).pipe(
            map((res:Response) => res['data'])
        )
    }
    /** 删除岗位信息 */
    delete(roleId: number) {
        return this.http.get('/system/api/roleinfo/delete?roleId=' + roleId).pipe(
            map((res:Response) => res['data'])
        )
    }
    /** 根据岗位获取人员信息 */
    getHumansByRole(roleId: number) {
        return this.http.get('/system/api/roleinfo/gethumanbyrole?roleId=' + roleId).pipe(
            map((res:Response) => res['data'])
        )
    }
    /** 保存或更新人员岗位信息 */
    saveUpdateHumanRole(roleHumans: any) {
        return this.http.post('/system/api/roleinfo/saveupdaterolehuman', roleHumans).pipe(
            map((res:Response) => res['data'])
        )
    }
    /** 加载人员岗位资源信息 */
    loadRoleResources(roleId: number) {
        return this.http.get('/system/api/roleinfo/loadroleresources?roleId=' + roleId).pipe(
            map((res:Response) => res['data'])
        )
    }
    /** 保存岗位人员权限*/
    saveUpdateRoleResources(params : any){
        return this.http.post('/system/api/roleinfo/saveupdateroleresources', params).pipe(
            map((res:Response) => res['data'])
        )
    }
    /**父子组件通讯 */
    announceMission(res: any) {
        this.routeMsgPushSource.next(res);
    }
}
